﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Collections.ObjectModel;
using System.ComponentModel;

using System.Globalization;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        Random _rand;
        public MainWindow()
        {
            InitializeComponent();

            _rand = new Random();

            dataGrid1.DataContext = this;
        }

        private ObservableCollection<MyClass> _values;

        public ObservableCollection<MyClass> Values
        {
            get { return _values; }
            set 
            {
                if (_values == value)
                    return;

                _values = value;
                OnPropertyChanged("Values");
            }
        }
        
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (Values == null)
            {
                Values = new ObservableCollection<MyClass>();
                Values.Add(new MyClass { Value = 1, Value1 = 2 });
                Values.Add(new MyClass { Value = 2, Value1 = 3 });
                Values.Add(new MyClass { Value = 3, Value1 = 4 });
                Values.Add(new MyClass { Value = 4, Value1 = 4 });
            }
            else
            {
                foreach (var item in Values)
                {
                    MyClass c = item as MyClass;

                    if (_rand.Next(0, 2) == 1)
                    {
                        c.Value = _rand.Next(0, 100);
                    }

                    if (_rand.Next(0, 2) == 1)
                    {
                        c.Value1 = _rand.Next(0, 100);
                    }
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
}
